package com.yun.routing.topic;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DeliverCallback;
import com.yun.utils.ConnectionUtil;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class Consume {

    private static final String EXCHANGE_NAME = "topic_logs";
    public static void main(String[] args) throws IOException {
        Connection connection = ConnectionUtil.getConnection();

        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME,"topic");

        String queueName = channel.queueDeclare().getQueue();

        channel.queueBind(queueName,EXCHANGE_NAME,"*.topic.#");

        DeliverCallback deliverCallback = (tag,delivery) -> {
            String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
            System.out.println(delivery.getEnvelope().getRoutingKey()+":"+message);
        };

        channel.basicConsume(queueName,true,deliverCallback,(consumerTag, sig) -> {});
    }
}
